#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>

#define ARRAYENGINEENABLE  0	//使用数组作为存储引擎
#define RBTREEENGINEENABLE 0	//使用红黑树作为存储引擎
#define SKIPLISTENGINEENABLE 0	//使用跳表作为存储引擎
#define HASHENGINEENABLE 1	    //使用哈希表作为存储引擎

int testcase(int fd,char *send_msg,char *recv_msg,char *tip)
{
    char buff[1024] = {0};
    int num = send(fd,send_msg,strlen(send_msg),0);
    if (num == -1)
    {
        perror("send failed\n");
        return -1;
    }

    int size = recv(fd,buff,1024,0);
    if (num == -1)
    {
        perror("recv failed\n");
        return -1;
    }

    if (strcmp(recv_msg,buff)==0)
    {
        printf("%s passed\n",tip);
    }
    else
    {
        printf("%s no passed\n",tip);
        printf("recv:%s --- recv_msg:%s\n",buff,recv_msg);
    }
    
}

int main()
{
    int confd = socket(AF_INET,SOCK_STREAM,0);

    struct sockaddr_in sockserver_addr;
    sockserver_addr.sin_family = AF_INET;
    sockserver_addr.sin_addr.s_addr = INADDR_ANY;
    sockserver_addr.sin_port = htons(32767);

    if(connect(confd,(struct sockaddr*)&sockserver_addr,sizeof(struct sockaddr_in))!=0)
    {
        perror("connect failed\n");
        return -1;
    }
#if ARRAYENGINEENABLE
    testcase(confd,"SET user zhw","OK\n","SET Command:");
    testcase(confd,"SET user zhw","EXIST\n","SET Repeat Command:");
    testcase(confd,"GET user","zhw\n","GET user:");
    testcase(confd,"GET user1","NO EXIST\n","GET user1:");
    testcase(confd,"EXIST user","YES\n","EXIST user:");
    testcase(confd,"EXIST user1","NO\n","EXIST user1:");
    testcase(confd,"MOD user CHX","OK\n","MOD user:");
    testcase(confd,"GET user","CHX\n","GET user:");
    testcase(confd,"DEL user","OK\n","DEL user:");
    testcase(confd,"GET user","NO EXIST\n","GET user:");
#elif RBTREEENGINEENABLE
    testcase(confd,"RSET user zhw","OK\n","SET Command:");
    testcase(confd,"RSET user zhw","EXIST\n","SET Repeat Command:");
    testcase(confd,"RGET user","zhw\n","GET user:");
    testcase(confd,"RGET user1","NO EXIST\n","GET user1:");
    testcase(confd,"REXIST user","YES\n","EXIST user:");
    testcase(confd,"REXIST user1","NO\n","EXIST user1:");
    testcase(confd,"RMOD user CHX","OK\n","MOD user:");
    testcase(confd,"RGET user","CHX\n","GET user:");
    testcase(confd,"RDEL user","OK\n","DEL user:");
    testcase(confd,"RGET user","NO EXIST\n","GET user:");
#elif SKIPLISTENGINEENABLE
    testcase(confd,"SSET user zhw","OK\n","SET Command:");
    testcase(confd,"SSET user zhw","EXIST\n","SET Repeat Command:");
    testcase(confd,"SGET user","zhw\n","GET user:");
    testcase(confd,"SGET user1","NO EXIST\n","GET user1:");
    testcase(confd,"SEXIST user","YES\n","EXIST user:");
    testcase(confd,"SEXIST user1","NO\n","EXIST user1:");
    testcase(confd,"SMOD user CHX","OK\n","MOD user:");
    testcase(confd,"SGET user","CHX\n","GET user:");
    testcase(confd,"SDEL user","OK\n","DEL user:");
    testcase(confd,"SGET user","NO EXIST\n","GET user:");

#elif HASHENGINEENABLE
    testcase(confd,"HSET user zhw","OK\n","SET Command:");
    testcase(confd,"HSET user zhw","EXIST\n","SET Repeat Command:");
    testcase(confd,"HGET user","zhw\n","GET user:");
    testcase(confd,"HGET user1","NO EXIST\n","GET user1:");
    testcase(confd,"HEXIST user","YES\n","EXIST user:");
    testcase(confd,"HEXIST user1","NO\n","EXIST user1:");
    testcase(confd,"HMOD user CHX","OK\n","MOD user:");
    testcase(confd,"HGET user","CHX\n","GET user:");
    testcase(confd,"HDEL user","OK\n","DEL user:");
    testcase(confd,"HGET user","NO EXIST\n","GET user:");

#endif
    close(confd);

    return 0;
}